package com.rf.richfitwheel.admin.conf;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 对接门户使用的配置
 * @author hushouquan
 */
@Component
@ConfigurationProperties("richfitwheel.common.iam")
public class IamPropertiesConfig{
    /**
     * 接口地址
     */
    private String url;
    private String csbUrl;
    /**
     * 我们有多个系统对接，各个系统都有自己的配置，所以使用集合获取配置
     */
    List<AppAccess> appAccess = new ArrayList<>();
    
    public String getUrl(){
        return url;
    }
    
    public void setUrl(String url){
        this.url = url;
    }
    
    public String getCsbUrl(){
        return csbUrl;
    }
    
    public void setCsbUrl(String csbUrl){
        this.csbUrl = csbUrl;
    }
    
    public List<AppAccess> getAppAccess(){
        return appAccess;
    }
    
    public void setAppAccess(List<AppAccess> appAccess){
        this.appAccess = appAccess;
    }
    
    public HttpEntity getHttpEntity(String appId){
        HttpHeaders requestHeaders = new HttpHeaders();
//        List<AppAccess> appAccess = getAppAccess();
        AppAccess acc = this.appAccess.stream().filter(m -> m.getAppId().equals(appId)).findFirst().get();
        requestHeaders.set("version", "1.0.1");
        requestHeaders.set("accessKey", acc.getAccessKey());
        requestHeaders.set("secretKey", acc.getSecretKey());
        requestHeaders.setContentType(MediaType.parseMediaType("application/x-www-form-urlencoded;charset=utf-8"));
        return new HttpEntity(requestHeaders);
    }
    
    public String getAccessKeyByAppId(String appId){
        AppAccess acc = this.appAccess.stream().filter(m -> m.getAppId().equals(appId)).findFirst().get();
        return acc.getAccessKey();
    }
    
    public String getSecretKeyByAppId(String appId){
        AppAccess acc = this.appAccess.stream().filter(m -> m.getAppId().equals(appId)).findFirst().get();
        return acc.getSecretKey();
    }
    
    public String getHostnameByAppId(String appId){
        AppAccess acc = this.appAccess.stream().filter(m -> m.getAppId().equals(appId)).findFirst().get();
        return acc.getHostname();
    }
    
    public String getBusinessTypeByAppId(String appId){
        AppAccess acc = this.appAccess.stream().filter(m -> m.getAppId().equals(appId)).findFirst().get();
        return acc.getBusinessType();
    }
}
